#include "ticketmanagement.h"
#include <iostream>
#include "app_mysql.h"

using namespace std;

Ticket TicketManagement::ticket;

QString* TicketManagement::addTicket(QString trainNum)
{
    //call query TRAIN method assign to train

    Train train;

    database_start("root", "");
    char *ch;
    ch = trainNum.toLocal8Bit().data();
    train = queryTrain_mysql(ch);
    database_end();

    ticket.trainNum = trainNum;

    QString* stationList = new QString[train.length()+1];

    stationList[0] = train.length();
    Station tempS;
    QTime tempT;
    for(int i = 0; i < train.length(); i++)
    {
        train.getItem(tempS,tempT,i);
        stationList[i+1] = tempS.name;
    }

    return stationList;
}

QString TicketManagement::queryTicket(QString ticketID)
{
    //call query ticket method assign to ticket
    database_start("root", "");
    int id;
    id = ticketID.toInt();
    ticket = queryTicketType_mysql(id);
    database_end();

    cout << ticket.ID.toLocal8Bit().data() << ticket.trainNum.toLocal8Bit().data() << ticket.startStationIndex
         << ticket.arrivalStationIndex << ticket.seatNum << ticket.price << endl;

    return ticket.trainNum;
}

void TicketManagement::deleteTicket()
{
    //call delete methd ticket.ID
    database_start("root", "");
    int id;
    id = ticket.trainNum.toInt();
    deleteTicketType_mysql(id);
    database_end();
}

void TicketManagement::modifyTicket(int startIndex, int arrivalIndex, int seatNum, double price)
{
    ticket.startStationIndex = startIndex;
    ticket.arrivalStationIndex = arrivalIndex;
    ticket.seatNum = seatNum;
    ticket.price = price;

    //call modify method

    char *num = ticket.trainNum.toLocal8Bit().data();

    database_start("root", "");
    deleteTicketType_mysql(ticket.ID.toInt());
    addTicketType_mysql(num,startIndex,arrivalIndex,seatNum,price);
    database_end();
}
